package com.lujianfei.gamelearn.mahjong

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.lujianfei.gamelearn.MyApplication

class MahjongQuestionManager private constructor() {

    companion object {
        val instance by lazy { MahjongQuestionManager() }
    }

    val resultList = arrayListOf<QuestionModel>()
    /***
     * 加载麻将题目
     */
    fun load():List<QuestionModel> {
        resultList.clear()
        /**
         * 加载 assets 内容
         */
        MyApplication.instance.assets.open("mahjong.json").use {
            val json = it.bufferedReader().use { it.readText() }
            val gson = Gson()
            val type = object : TypeToken<List<QuestionModel>>() {}.type
            val questionList = gson.fromJson<List<QuestionModel>>(json, type)
            questionList.forEach {
                // 生成题目
                resultList.add(QuestionModel(it.cards, it.answer, it.commentate))
            }
        }
        return resultList
    }
}



/// 出题
data class QuestionModel(val cards: List<Card>, val answer: List<Card>, val commentate:String)

data class Card(val type:String, val number:Int) {
    fun toName(): String {
        val fengMap = mapOf(1 to "东", 2 to "南", 3 to "西", 4 to "北")
        val sanyuanMap = mapOf(1 to "中", 2 to "发", 3 to "白")
        return when (type) {
            "wan" -> "${number}万"
            "tong" -> "${number}筒"
            "tiao" -> "${number}条"
            "feng" -> "${fengMap[number]}风"
            "sanyuan" -> "${sanyuanMap[number]}"
            else -> ""
        }
    }
}
